package com.itheima.reggie.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

// 购物车(ShoppingCart)表控制层

@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    /**
     * 服务对象
     */
    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 添加进购物车
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //1.设置用户id，指定当前是哪个用户的购物车数据
        //1 获取id：BaseContext.getCurrentId()
        Long id = BaseContext.getCurrentId();
        //2 将获取到的id赋值给参数
        shoppingCart.setUserId(id);
        //3 菜品id
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
      objectLambdaQueryWrapper.eq(ShoppingCart::getUserId, id);
        //判断购物车中的是菜品还是套餐
       if (dishId != null) {
            //不为空表示操作的是菜品信息
            objectLambdaQueryWrapper.eq(ShoppingCart::getDishId, dishId);
       } else {
            //表示操作的是套餐信息
            objectLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
       }
        //查询当前商品是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(objectLambdaQueryWrapper);

        if (cartServiceOne != null) {
            //如果已经存在，就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            //创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);

    }


    /**
     * 查看购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //创建构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //eq.UserId
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //orderByAse
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);

    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(objectLambdaQueryWrapper);
        return R.success("清空成功");
    }

    /**
     * 修改购物车商品数量
     */
    @PostMapping("/sub")

    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //获取菜品的id
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        //代表数量减少的是菜品数量
        if (dishId != null){
            //通过dishId查出购物车对象
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            //这里必须要加两个条件࿰c;否则会出现用户互相修改对方与自己购物车中相同套餐或者是菜品的数量
            queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            ShoppingCart cart1 = shoppingCartService.getOne(queryWrapper);
            cart1.setNumber(cart1.getNumber()-1);
            Integer Latestnumber = cart1.getNumber();
            if (Latestnumber > 0){
                //对数据进行更新操作
                shoppingCartService.updateById(cart1);
            }else if(Latestnumber == 0){
                //如果购物车的菜品数量减为0࿰c;那么就把菜品从购物车删除
                shoppingCartService.removeById(cart1.getId());
            }else if (Latestnumber < 0){
                return R.error("操作异常");
            }
            return R.success(cart1);
        }
        Long setmealId = shoppingCart.getSetmealId();
        if (setmealId != null){
            //代表是套餐数量减少
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId).eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            ShoppingCart cart2 = shoppingCartService.getOne(queryWrapper);
            cart2.setNumber(cart2.getNumber()-1);
            Integer Latestnumber = cart2.getNumber();
            if (Latestnumber > 0){
                //对数据进行更新操作
                shoppingCartService.updateById(cart2);
            }else if(Latestnumber == 0){
                //如果购物车的套餐数量减为0࿰c;那么就把套餐从购物车删除
                shoppingCartService.removeById(cart2.getId());
            }else if (Latestnumber < 0){
                return R.error("操作异常");
            }
            return R.success(cart2);
        }
        //如果两个大if判断都进不去
        return R.error("操作异常");
    }
}

